import permision from "@/js_sdk/wa-permission/permission.js"
/**
 * 判断是否为空 空为true
 */
export function validateNull(val) {
    if (val instanceof Array && Array.isArray(val)) {
        if (val.length === 0) return true;
    } else if (val instanceof Object) {
        if (JSON.stringify(val) === '{}') return true;
    } else {
        if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') {
            return true;
        }
        return false;
    }
    return false;

}


export function openAppSettingPermissions() {
    console.log('%c [ openAppSettingPermissions ]-21', 'font-size:13px; background:pink; color:#bf2c9f;', 'openAppSettingPermissions')
    uni.openAppAuthorizeSetting({
        success(res) {
            console.log(res)
        }
    })
}

// vue的method里编写如下代码
export async function requestAndroidPermission(permisionID) {
    var result = await permision.requestAndroidPermission(permisionID)
    var strStatus
    if (result == 1) {
        // strStatus = "已获得授权"
        strStatus = 1
    } else if (result == 0) {
        // strStatus = "未获得授权"
        strStatus = 0
    } else {
        // strStatus = "被永久拒绝权限"
        strStatus = -1
    }
    return strStatus
}

export function hideMobile(mobile) {
    if (validateNull(mobile)) return ''
    console.log('%c [ mobile.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2") ]-50', 'font-size:13px; background:pink; color:#bf2c9f;', mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'))
    let before = mobile.substr(0, 3)
    let after = mobile.substr(7, 4)
    return before + '****' + after
    // return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

export function pushMessageReceiveClick() {
    plus.push.addEventListener("receive", function (msg) {
        console.log('%c 收到消息推送 msg ]-30', 'font-size:13px; background:pink; color:#bf2c9f;', msg)
        if (uni.getSystemInfoSync().platform === 'ios') {
            if (msg.type === 'receive') {
                if (msg.content && msg.title) {
                    plus.push.createMessage(msg.payload.body, msg.payload, {
                        title: msg.payload.title
                    });
                }
            }

        } else if (uni.getSystemInfoSync().platform === 'android') {
            // if (msg.content && msg.title) {
            //     let content = JSON.parse(msg.content)
            //     console.log('%c [ content ]-68', 'font-size:13px; background:pink; color:#bf2c9f;', content)
            //     plus.push.createMessage('', '', {
            //         title: ''
            //     });
            // }
        }
    })
    //点击事件
    plus.push.addEventListener("click", (payload) => {
        console.log('%c [ 点击消息推送payload ]-37', 'font-size:13px; background:pink; color:#bf2c9f;', payload)
        uni.switchTab({
            url: '/pages/index/xiaoxi'
        })
    });
}